package com.taobaobao.com.taobaobao;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.taobaobao.com.taobaobao.base.BaseActivity;
import com.taobaobao.com.taobaobao.base.BaseFragment;
import com.taobaobao.com.taobaobao.module.home.HomeFragment;
import com.taobaobao.com.taobaobao.module.shoppingcart.ShoppingCartFragment;
import com.taobaobao.com.taobaobao.module.空白碎片;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements BottomNavigationBar.OnTabSelectedListener {

    private BottomNavigationBar bottomNavigationBar;

    private List<BaseFragment> tabFragments = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFragments();
        initBottomBar();
        onTabSelected(0);
    }

    private void initBottomBar() {
        bottomNavigationBar = findViewById(R.id.bottom_navigation_bar);
        bottomNavigationBar.setTabSelectedListener(this);
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);
        bottomNavigationBar.addItem(new BottomNavigationItem(R.drawable.ic_launcher_background, "首页"))
                .addItem(new BottomNavigationItem(R.drawable.ic_launcher_background, "微淘"))
                .addItem(new BottomNavigationItem(R.drawable.ic_launcher_background, "消息"))
                .addItem(new BottomNavigationItem(R.drawable.ic_launcher_background, "购物车"))
                .addItem(new BottomNavigationItem(R.drawable.ic_launcher_background, "我的"))
                .setFirstSelectedPosition(0)
                .initialise(); //所有的设置需在调用该方法前完成
    }

    //选中
    @Override
    public void onTabSelected(int position) {
            initFragment(position);
    }

    //未选中
    @Override
    public void onTabUnselected(int position) {

    }

    //再次选中
    @Override
    public void onTabReselected(int position) {

    }

    private void initFragments() {
        tabFragments.add(new HomeFragment());
        tabFragments.add(new 空白碎片());
        tabFragments.add(new 空白碎片());
        tabFragments.add(new ShoppingCartFragment());
        tabFragments.add(new 空白碎片());
    }

    //fragment init
    private void initFragment(int p) {
        FragmentManager fm = this.getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        transaction.replace(R.id.main_framelayout, tabFragments.get(p));
        transaction.commit();
    }

}
